package cn.bdqn.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
//import javax.jws.WebParam.Mode;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.bdqn.pojo.Answer;
import cn.bdqn.pojo.Question;
import cn.bdqn.service.AnswerService;
import cn.bdqn.service.QuestionService;

@Controller
public class QuestionController {
	@Resource(name="questionService")
	private QuestionService quService;
	
	@Resource(name="answerService")
	private AnswerService anService;
	
	//问题列表
	@RequestMapping("/qlist.html")
	public String qlist(Model model){
		List<Question> qList=quService.qlist();
		model.addAttribute("qList", qList);
		return "qlist";
	}
	
	//我要提问
	@RequestMapping("/myquestion.html")
	public String myquestion(){
		return "myquestion";
	}
	
	//问题保存
	@RequestMapping("/save.html")
	public String save(@RequestParam(value="title")String title,
			@RequestParam(value="detailDesc")String detailDesc){
		Question question=new Question();
		question.setAnswerCount(0);
		question.setDetailDesc(detailDesc);
		Date date=new Date();
		SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd");
		String lastModified=sDateFormat.format(date);
		question.setLastModified(lastModified);
		question.setTitle(title);
		quService.addQus(question);
		return "forward:/qlist.html";
	}
	
	//我要回答
	@RequestMapping("/search.html")
	public String search(@RequestParam("id")String id,Model model){
		Question question=new Question();
		question.setId(Integer.valueOf(id));
		question=quService.getQue(question);
		model.addAttribute("question", question);
		return "search";
	}
	
	//回答保存}/saveans.html
	@RequestMapping("/saveans.html")
	public String saveans(@RequestParam("id")String id,
			@RequestParam("ansContent")String ansContent){
		Date date=new Date();
		SimpleDateFormat sFormat=new SimpleDateFormat("yyyy-MM-dd");
		String ansDate=sFormat.format(date);
		Answer answer=new Answer();
		answer.setAnsContent(ansContent);
		answer.setAnsDate(ansDate);
		answer.setQid(Integer.valueOf(id));
		anService.addAns(answer);
		Question question=new Question();
		question.setId(Integer.valueOf(id));
		question=quService.getQue(question);
		question.setLastModified(ansDate);
		question.setAnswerCount(question.getAnswerCount()+1);
		quService.modifyAns(question);
		return "forward:/qlist.html";
	}
}
